package com.kaigejava.service.impl;

import com.kaigejava.domain.Salary;
import com.kaigejava.mapper.SalaryMapper;
import com.kaigejava.service.ISalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by kaigejava on 2019/7/22.
 */
@Service("salaryService")
public class SalaryServiceImpl implements ISalaryService {

    @Autowired
    private SalaryMapper salaryMapper;
    @Override
    public List<Salary> getSalaryByDate(String start, String end) throws ParseException {
        List<Salary> dateList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(start);
        Date endDate = sdf.parse(end);
        int days = DateUtils.dateBetweenIncludeToday(startDate,endDate);
        //查询
        List<Salary> list = salaryMapper.getSalaryByDate(start,end);
        List<String> pageDateList = new ArrayList<>();
        for(int i =  0 ;i<days;i++){
            Date addOne = DateUtils.dateAddDays(startDate,i+1);
            pageDateList.add(sdf.format(addOne));
        }
        if(null == list || list.isEmpty()){
            for(int i =  0 ;i<days;i++){
                Salary salary = new Salary();
                salary.setSalaryMoney(0.0F);
                salary.setCommission(0.0F);
                Date addOne = DateUtils.dateAddDays(startDate,i+1);
                salary.setSalaryDate(sdf.format(addOne));
                dateList.add(salary);
            }
        }else{
            Map<String,Salary> dataMap = new HashMap<>();
            List<String> salaryDateList = new ArrayList<>();
            for(Salary salary:list){
                String salaryDate = salary.getSalaryDate();
                if(null != dataMap){
                    if(dataMap.containsKey(salaryDate)){
                        Salary mapValue = dataMap.get(salaryDate);
                        Float Commission = mapValue.getCommission()+salary.getCommission();
                        Float salaryMoney = mapValue.getSalaryMoney()+salary.getSalaryMoney();
                        mapValue.setCommission(Commission);
                        mapValue.setSalaryMoney(salaryMoney);
                        dataMap.put(salaryDate,mapValue);
                    }else{
                        dataMap.put(salaryDate,salary);
                        salaryDateList.add(salaryDate);
                    }
                }else{
                    salaryDateList.add(salaryDate);
                    dataMap.put(salaryDate,salary);
                }
            }
            //比较日期
            int dbSize = salaryDateList.size();
            if(days>dbSize){    //库中不存在的
                for(String key:pageDateList){
                    if(dataMap.containsKey(key)){
                        dateList.add(dataMap.get(key));
                    }else{  //构造
                        Salary salary = new Salary();
                        salary.setSalaryDate(key);
                        salary.setSalaryMoney(0.0F);
                        salary.setCommission(0.0F);
                        dateList.add(salary);
                    }
                }
            }else{  //相等
                for(String key :salaryDateList){
                    dateList.add(dataMap.get(key));
                }

            }
        }
        return dateList;
    }
}
